package com.anne.fish.webapp.interceptor;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.anne.fish.Constants;
import com.anne.fish.webapp.listener.StartupListener;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class CacheUpdateInterceptor extends AbstractInterceptor {

	 
	public static final long time = 1000 * 60 * 30;

	public static final long time2 = 1000 * 60 * 60 * 3;
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		updateFrontData(request);
		
		return invocation.invoke();
	}

	
	public static void updateFrontData(HttpServletRequest request){
		ServletContext context = request.getSession().getServletContext();
		
		Long lastUpdate  =(Long) context.getAttribute(Constants.CACHE_LAST_UPDATE);
		
		long now = System.currentTimeMillis();
		if( now- time > lastUpdate){ 
			StartupListener.setupContext(context);	        
	        context.setAttribute(Constants.CACHE_LAST_UPDATE, now); 
		}
		
 
	}

}
